#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4

import logging
from lxml import etree
from cvirt.cnode.libvirtXmlObject.baseDomain import GuestDevice

LOG = logging.getLogger(__name__)

class virtualRedirdev(GuestDevice):
    ROOT_NAME = 'redirdev'
    def __init__(self, parse_xml=None, **kwargs):
        self.type = None
        self.bus = None
        self.address = None
        super(virtualRedirdev, self).__init__(parse_xml=parse_xml, **kwargs)

    def _parse_xml(self, xmldoc):
        self.type = xmldoc.get('type')
        self.bus = xmldoc.get('bus')
        for c in xmldoc.getchildren():
            if c.tag == 'address':
                self.address = virtualDeviceAddress(parse_xml=etree.tostring(c))

    def _get_xml_config(self, root):
        if self.type:
            root.set('type',self.type)
        if self.bus:
            root.set('bus',self.bus)
        if self.address:
            root.append(self.address.get_xml_config())
        return root

class virtualController(GuestDevice):
    ROOT_NAME = 'controller'
    def __init__(self, parse_xml=None, **kwargs):
        self.type = None
        self.index = None
        self.model = None
        self.address = None
        self.master = None
        super(virtualController, self).__init__(parse_xml=parse_xml, **kwargs)

    def _parse_xml(self, xmldoc):
        self.type = xmldoc.get('type')
        self.index = xmldoc.get('index')
        self.model = xmldoc.get('model')
        for c in xmldoc.getchildren():
            if c.tag == 'master':
                self.master = virtualDeviceMaster(parse_xml=etree.tostring(c))
            if c.tag == 'address':
                self.address = virtualDeviceAddress(parse_xml=etree.tostring(c))

    def _get_xml_config(self, root):
        if self.type:
            root.set('type',self.type)
        if self.index:
            root.set('index',self.index)
        if self.model:
            root.set('model',self.model)
        if self.address:
            root.append(self.address.get_xml_config())
        if self.master:
            root.append(self.master.get_xml_config())
        return root

class virtualDeviceMaster(GuestDevice):
    ROOT_NAME = 'master'
    def __init__(self, parse_xml=None, **kwargs):
        self.startport = None
        super(virtualDeviceMaster, self).__init__(parse_xml=parse_xml, **kwargs)
    def _parse_xml(self, xmldoc):
        self.startport = xmldoc.get('startport')

    def _get_xml_config(self, root):
        root.set('startport',self.startport)
        return root

class virtualDeviceAddress(GuestDevice):
    ROOT_NAME = 'address'
    def __init__(self, parse_xml=None, **kwargs):
        # PCI address:
        # <address type='pci' domain='0x0000' bus='0x00' slot='0x04' \
        #                     function='0x0'/>
        self.type = None
        self.domain = None
        self.bus = None
        self.slot = None
        self.function = None

        # VirtioSerial address:
        # <address type='virtio-serial' controller='1' bus='0' port='4'/>
        self.port = None

        self.multifunction = None
        super(virtualDeviceAddress, self).__init__(parse_xml=parse_xml, **kwargs)

    def _parse_xml(self, xmldoc):
        self.type = xmldoc.get('type')
        self.domain = xmldoc.get('domain')
        self.bus = xmldoc.get('bus')
        self.slot = xmldoc.get('slot')
        self.function = xmldoc.get('function')

        self.port = xmldoc.get('port')

        self.multifunction = xmldoc.get('multifunction')

    def _get_xml_config(self, root):
        if self.type:
            root.set('type',self.type)
        if self.domain:
            root.set('domain',self.domain)
        if self.bus:
            root.set('bus',self.bus)
        if self.slot:
            root.set('slot',self.slot)
        if self.function:
            root.set('function',self.function)
        if self.port:
            root.set('port',self.port)
        if self.multifunction:
            root.set('multifunction',self.multifunction)
        return root

